package com.ucarbon.ucarbon.service.impl;

import com.ucarbon.ucarbon.entity.FunctionValues;
import com.ucarbon.ucarbon.entity.YearCountryNpp;
import com.ucarbon.ucarbon.mapper.YearCountryNppMapper;
import com.ucarbon.ucarbon.service.IYearCountryNppService;
import com.ucarbon.ucarbon.utils.EchartsOptionsGnerator;
import com.ucarbon.ucarbon.utils.RespBean;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 区县, 年份对应NPP的表格 服务实现类
 * </p>
 *
 * @author XuGaoran
 * @since 2024-01-04
 */
@Service
public class YearCountryNppServiceImpl extends ServiceImpl<YearCountryNppMapper, YearCountryNpp>
        implements IYearCountryNppService {

    @Autowired
    private YearCountryNppMapper yearCountryNppMapper;

    @Override
    public RespBean selectByYear(Integer year) {
        try {
            return RespBean.success("成功获取结果",
                    EchartsOptionsGnerator.generateBarOptions(
                            year, yearCountryNppMapper.selectByYear(year)));
        } catch (Exception e) {
            return RespBean.error("未能成功生成数据", e);
        }
    }

    @Override
    public RespBean selectByCountry(String country) {
        try {
            return RespBean.success("成功返回通过年份查询城市数据的结果",
                    EchartsOptionsGnerator.generatePlotOptions(
                        country, yearCountryNppMapper.selectByCountry(country)));
        } catch (Exception e) {
            return RespBean.error("未能成功生成数据", e);
        }
    }

    @Override
    public RespBean selectFuncValue(String country, Integer year) {
        try {
            Map<String, FunctionValues> listFv = new HashMap<>();
            listFv.put(country, yearCountryNppMapper.selectValueByCountry(country));
            listFv.put(year.toString(), yearCountryNppMapper.selectValueByYear(year));
            return RespBean.success("成功返回相关数据", listFv);
        } catch (Exception e) {
            return RespBean.error("未能成功生成数据", e);
        }
    }

}
